package cn.john.org.service.impl;

import cn.john.basic.exception.BusinessException;
import cn.john.basic.service.impl.BaseServiceImpl;
import cn.john.org.domain.Employee;
import cn.john.org.domain.Shop;
import cn.john.org.mapper.EmployeeMapper;
import cn.john.org.mapper.ShopMapper;
import cn.john.org.service.IEmployeeService;
import cn.john.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    /* 1. extends BaseServiceImpl<Employee> : 实现基础接口中的方法 */

    /* 2. 实现扩展接口 */
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Override
    @Transactional
    public void settledIn(Shop shop) {
        Employee admin = shop.getAdmin();
        /* 判断用户是否已经存在 */
        Employee employee = employeeMapper.loadByUser(admin);
        if (employee != null) {
            throw new BusinessException("已经入驻过，请直接登录，如果忘记密码，请找回密码");
        }
        /* 如果不存在，就走入驻 */
        /* 保存管理员信息并返回自增id,业务交给service去做 */
        employeeService.add(admin);
        shopMapper.save(shop);
        /* 设置关联shop字段，然后更新修改员工信息 */
        admin.setShop(shop);
        employeeMapper.update(admin);
    }
}
